package com.it.hanzhongtravelv1.service;

import com.it.hanzhongtravelv1.dto.DayActivityDTO;
import com.it.hanzhongtravelv1.dto.RouteDayDTO;
import com.it.hanzhongtravelv1.dto.TravelRouteDTO;
import com.it.hanzhongtravelv1.entity.DayActivity;
import com.it.hanzhongtravelv1.entity.RouteDay;
import com.it.hanzhongtravelv1.entity.TravelRoute;
import com.it.hanzhongtravelv1.repository.TravelRouteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TravelRouteService {

    @Autowired
    private TravelRouteRepository travelRouteRepository;

    public List<TravelRouteDTO> getAllTravelRoutes() {
        return travelRouteRepository.findAll().stream()
                .map(this::convertToTravelRouteDto)
                .collect(Collectors.toList());
    }

    public List<TravelRouteDTO> getTravelRoutesByDuration(String duration) {
        // Assuming duration matches "一日游", "三日游", etc.
        String durationKey = duration;
        if ("oneday".equalsIgnoreCase(duration)) {
            durationKey = "一日游";
        } else if ("threedays".equalsIgnoreCase(duration)) {
            durationKey = "三日游";
        } else if ("fivedays".equalsIgnoreCase(duration)) {
            durationKey = "五日游";
        }
        
        return travelRouteRepository.findByDuration(durationKey).stream()
                .map(this::convertToTravelRouteDto)
                .collect(Collectors.toList());
    }

    public TravelRouteDTO convertToTravelRouteDto(TravelRoute route) {
        TravelRouteDTO dto = new TravelRouteDTO();
        dto.setId(route.getId());
        dto.setName(route.getName());
        dto.setDescription(route.getDescription());
        dto.setDuration(route.getDuration());
        dto.setTheme(route.getTheme());
        dto.setPrice(route.getPrice());
        dto.setTransportation(route.getTransportation());
        dto.setImage(route.getImage());
        dto.setTips(route.getTips());
        dto.setFeatures(route.getFeatures());
        dto.setSuitableFor(route.getSuitableFor());
        dto.setDays(route.getDays().stream().map(this::convertToRouteDayDto).collect(Collectors.toList()));
        return dto;
    }

    private RouteDayDTO convertToRouteDayDto(RouteDay routeDay) {
        RouteDayDTO dto = new RouteDayDTO();
        dto.setDayNumber(routeDay.getDayNumber());
        dto.setActivities(routeDay.getActivities().stream().map(this::convertToDayActivityDto).collect(Collectors.toList()));
        return dto;
    }

    private DayActivityDTO convertToDayActivityDto(DayActivity dayActivity) {
        DayActivityDTO dto = new DayActivityDTO();
        dto.setTimeType(dayActivity.getTimeType());
        dto.setTime(dayActivity.getTime());
        dto.setTitle(dayActivity.getTitle());
        dto.setDescription(dayActivity.getDescription());
        return dto;
    }
} 